package com.hudongwx.my_shian.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.hudongwx.my_shian.R;
import com.hudongwx.my_shian.fragment.CommunityFragment;
import com.hudongwx.my_shian.fragment.HomePageFragment;
import com.hudongwx.my_shian.fragment.MyFragment;
import com.hudongwx.my_shian.fragment.ShoppingCartFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private FragmentManager manager;
    Fragment[] fragments = new Fragment[4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

//        View inflate = getLayoutInflater().inflate(R.layout.fragment_home_page, null);

        //1.导包，在布局文件中添加控件，查找控件
        tabLayout = (TabLayout) findViewById(R.id.tabLayout_widget);
        //2.绑定控件，显示碎片
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_tabLayout, new HomePageFragment());
        transaction.commit();

        //给每个选项可设置内容，并且让第一个选项卡默认选中
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.selector_home).setText("主页").setTag("tag1"));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.selector_community).setText("社区").setTag("tag2"));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.selector_shopping).setText("购物车").setTag("tag3"));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.selector_my).setText("我的").setTag("tag4"));

        //设置选项卡的显示方式//所有选项卡平分父视图
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //TabLayout的监听事件
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //选中时调用该方法
                if (tab.getTag().equals("tag1")) {
                    if (null == fragments[0])
                        fragments[0] = new HomePageFragment();
                    FragmentTransaction transaction1 = manager.beginTransaction();
                    transaction1.replace(R.id.fragment_tabLayout, fragments[0]);
                    transaction1.commit();
                } else if (tab.getTag().equals("tag2")) {
                    if (null == fragments[1])
                        fragments[1] = new CommunityFragment();
                    FragmentTransaction transaction1 = manager.beginTransaction();
                    transaction1.replace(R.id.fragment_tabLayout, fragments[1]);
                    transaction1.commit();
                } else if (tab.getTag().equals("tag3")) {
                    if (null == fragments[2])
                        fragments[2] = new ShoppingCartFragment();
                    FragmentTransaction transaction1 = manager.beginTransaction();
                    transaction1.replace(R.id.fragment_tabLayout, fragments[2]);
                    transaction1.commit();
                } else if (tab.getTag().equals("tag4")) {
                    if (null == fragments[3])
                        fragments[3] = new MyFragment();
                    FragmentTransaction transaction1 = manager.beginTransaction();
                    transaction1.replace(R.id.fragment_tabLayout, fragments[3]);
                    transaction1.commit();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }


}
